#include <bits/stdc++.h>
using namespace std;

int returndigit(long long k) {
    int digit = 0;
    while(k) {
        k /= 10;
        digit++;
    }
    return digit;
}

int main() {
    int t;
    cin >> t;
    while(t--) {
        string s = "";
        cin >> s;
        long long k;
        cin >> k;
        if(s[0] == '0') {
            cout << "-1" << endl;
            continue;
        }
        int n = s.length();
        int makeup = 0;
        int qmark = 0;
        for(int i = n-1; i >= 0; --i) {
            if(s[i] == '?') qmark++;
        }
        int digit = returndigit(k);
        if(s[0] == '?') {
            if(qmark <= digit) {
                cout << "-1" << endl;
                continue;
            }
        }else {
            if(qmark < digit) {
                cout << "-1" << endl;
                continue;
            }
        }
        string await_insert = to_string(k-1);
        for(int i = n-1; i >= 0; --i) {
            if(s[i] == '?') {
                if(await_insert.length() > 0) {
                    s[i] = await_insert.back();
                    await_insert.pop_back();
                }else {
                    if(i == 0) {
                        s[i] = '1';
                    }
                    else s[i] = '0';
                }
            }
        }
        cout << s << endl;
    }
}